﻿using System.Management;

namespace Monkey.License
{
    public class License
    {
        public static bool HasLicense(string profile)
        {
            switch (profile)
            {
                case "HienNguyen":
                    return true;//IsValidHardware(Profiles.HienNguyenProfile);

                case "Thehemoi":
                    return IsValidHardware(Profiles.ThehemoiProfile);

                case "Vinatech":
                    return IsValidHardware(Profiles.VinatechProfile);
            }

            return false;
        }

        private static bool IsValidHardware(HardwareProfile hardwareProfile)
        {
            return (GetCpuId() == hardwareProfile.CpuId)
                && (GetVolumeSerial() == hardwareProfile.VolumeSerial);
        }

        //private static string GetComputerModel()
        //{
        //    var s1 = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
        //    foreach (ManagementObject oReturn in s1.Get())
        //    {
        //        return oReturn["Model"].ToString().Trim();
        //    }
        //    return string.Empty;
        //}

        private static string GetCpuId()
        {
            var managClass = new ManagementClass("win32_processor");
            var managCollec = managClass.GetInstances();

            foreach (ManagementObject managObj in managCollec)
            {
                //Get only the first CPU's ID
                return managObj.Properties["processorID"].Value.ToString();
            }
            return string.Empty;
        }

        private static string GetVolumeSerial()
        {
            var disk = new ManagementObject(@"win32_logicaldisk.deviceid=""c:""");
            disk.Get();

            var volumeSerial = disk["VolumeSerialNumber"].ToString();
            disk.Dispose();

            return volumeSerial;
        }
    }
}
